home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap02 / GdiDemo3 / GdiDemo3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  5.1 KB  |  200 lines

  1. //***********************************************************************
  2. //
  3. //  GdiDemo3.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "GdiDemo3.h"
  9.  
  10. #define VHEIGHT (m_cyChar * 50)
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19.     m_pMainWnd = new CMainWindow;
  20.     m_pMainWnd->ShowWindow (m_nCmdShow);
  21.     m_pMainWnd->UpdateWindow ();
  22.     return TRUE;
  23. }
  24.  
  25. /////////////////////////////////////////////////////////////////////////
  26. // CMainWindow message map and member functions
  27.  
  28. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  29.     ON_WM_CREATE ()
  30. END_MESSAGE_MAP ()
  31.  
  32. CMainWindow::CMainWindow ()
  33. {
  34.     Create (NULL, "GdiDemo3");
  35. }
  36.  
  37. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  38. {
  39.     if (CFrameWnd::OnCreate (lpcs) == -1)
  40.         return -1;
  41.  
  42.     CMyView* pMyView = new CMyView;
  43.     pMyView->Create (NULL, NULL, WS_CHILD | WS_VISIBLE,
  44.             CRect (0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL);
  45.     return 0;   
  46. }
  47.     
  48. /////////////////////////////////////////////////////////////////////////
  49. // CMyView message map and member functions
  50.  
  51. BEGIN_MESSAGE_MAP (CMyView, CScrollView)
  52.     ON_WM_CREATE ()
  53.     ON_WM_SIZE ()
  54. END_MESSAGE_MAP ()
  55.     
  56. int CMyView::OnCreate (LPCREATESTRUCT lpcs)
  57. {
  58.     if (CScrollView::OnCreate (lpcs) == -1)
  59.         return -1;
  60.     
  61.     TEXTMETRIC tm;
  62.     CClientDC dc (this);
  63.     dc.GetTextMetrics (&tm);
  64.     m_cxChar = tm.tmAveCharWidth;
  65.     m_cyChar = tm.tmHeight;
  66.     return 0;
  67. }
  68.  
  69. void CMyView::OnSize (UINT nType, int cx, int cy)
  70. {
  71.     SetScrollSizes (MM_TEXT, CSize (0, VHEIGHT), CSize (0, cy),
  72.         CSize (0, m_cyChar));
  73. }
  74.  
  75. void CMyView::OnDraw (CDC* pDC)
  76. {
  77.     CPaintDC dc (this);
  78.                 
  79.     ShowPenStyles (pDC, m_cxChar * 2, m_cyChar);   
  80.     ShowPenWidths (pDC, m_cxChar * 2, m_cyChar * 15);
  81.     ShowBrushStyles (pDC, m_cxChar * 2, m_cyChar * 27);
  82. }
  83.  
  84. void CMyView::ShowPenStyles (CDC* pDC, int x, int y)
  85. {
  86.     static struct STYLES styles[] = {
  87.         PS_SOLID,       "PS_SOLID",
  88.         PS_DASH,        "PS_DASH",
  89.         PS_DOT,         "PS_DOT",
  90.         PS_DASHDOT,     "PS_DASHDOT",
  91.         PS_DASHDOTDOT,  "PS_DASHDOTDOT",
  92.         PS_NULL,        "PS_NULL",
  93.         PS_INSIDEFRAME, "PS_INSIDEFRAME"
  94.     };
  95.  
  96.     pDC->SetTextColor (RGB (0, 0, 0));
  97.     pDC->TextOut (x, y, "Pen Styles");
  98.  
  99.     int dy = (m_cyChar * 3) / 2;
  100.     int x1 = x + (m_cxChar * 2);
  101.     int x2 = x + (m_cxChar * 22);
  102.     int x3 = x + (m_cxChar * 46);
  103.  
  104.     CPen* pOldPen;
  105.     pDC->SetTextColor (RGB (255, 0, 0));
  106.  
  107.     for (int i=0; i<7; i++) {
  108.         y += dy;
  109.         pDC->TextOut (x1, y, styles[i].szStyleName);
  110.  
  111.         CPen pen (styles[i].nStyle, 1, RGB (255, 0, 0));
  112.         pOldPen = pDC->SelectObject (&pen);
  113.  
  114.         pDC->MoveTo (x2, y + (m_cyChar / 2));
  115.         pDC->LineTo (x3, y + (m_cyChar / 2));
  116.  
  117.         pDC->SelectObject (pOldPen);
  118.     }
  119. }
  120.  
  121. void CMyView::ShowPenWidths (CDC* pDC, int x, int y)
  122. {
  123.     static int nPenWidths[] = { 2, 8, 16, 24 };
  124.  
  125.     pDC->SetTextColor (RGB (0, 0, 0));
  126.     pDC->TextOut (x, y, "Pen Widths");
  127.  
  128.     int dy = m_cyChar * 2;
  129.     int x1 = x + (m_cxChar * 2);
  130.     int x2 = x + (m_cxChar * 22);
  131.     int x3 = x + (m_cxChar * 46);
  132.  
  133.     CPen* pOldPen;
  134.     CString strDescription;
  135.     pDC->SetTextColor (RGB (0, 0, 255));
  136.  
  137.     for (int i=0; i<4; i++) {
  138.         y += dy;
  139.         strDescription.Format ("%d Pixels", nPenWidths[i]);
  140.         pDC->TextOut (x1, y, strDescription);
  141.  
  142.         CPen pen (PS_SOLID, nPenWidths[i], RGB (0, 0, 255));
  143.         pOldPen = pDC->SelectObject (&pen);
  144.  
  145.         pDC->MoveTo (x2, y + (m_cyChar / 2));
  146.         pDC->LineTo (x3, y + (m_cyChar / 2));
  147.  
  148.         pDC->SelectObject (pOldPen);
  149.     }
  150. }
  151.  
  152. void CMyView::ShowBrushStyles (CDC* pDC, int x, int y)
  153. {
  154.     static struct STYLES styles[] = {
  155.         HS_BDIAGONAL,   "HS_BDIAGONAL",
  156.         HS_CROSS,       "HS_CROSS",
  157.         HS_DIAGCROSS,   "HS_DIAGCROSS",
  158.         HS_FDIAGONAL,   "HS_FDIAGONAL",
  159.         HS_HORIZONTAL,  "HS_HORIZONTAL",
  160.         HS_VERTICAL,    "HS_VERTICAL"
  161.     };
  162.  
  163.     pDC->SetTextColor (RGB (0, 0, 0));
  164.     pDC->TextOut (x, y, "Brush Styles");
  165.  
  166.     int dy = m_cyChar * 3;
  167.     int x1 = x + (m_cxChar * 2);
  168.     int x2 = x + (m_cxChar * 22);
  169.     int x3 = x + (m_cxChar * 46);
  170.  
  171.     CBrush* pOldBrush;
  172.  
  173.     for (int i=0; i<6; i++) {
  174.         y += dy;
  175.         pDC->TextOut (x1, y, styles[i].szStyleName);
  176.  
  177.         CRect rect (x2, y - m_cyChar, x3, y + m_cyChar);
  178.         CBrush brush (styles[i].nStyle, RGB (0, 255, 0));
  179.  
  180.         CPoint point (rect.left, rect.top);
  181.         pDC->LPtoDP (&point);
  182.         point.x %= 8;
  183.         point.y %= 8;
  184.         pDC->SetBrushOrg (point);
  185.  
  186.         pOldBrush = pDC->SelectObject (&brush);
  187.         pDC->Rectangle (rect);
  188.  
  189.         pDC->SelectObject (pOldBrush);
  190.     }
  191.  
  192.     y += dy;
  193.     pDC->TextOut (x1, y, "Solid");
  194.  
  195.     CBrush brush (RGB (0, 255, 0));
  196.     pOldBrush = pDC->SelectObject (&brush);
  197.     pDC->Rectangle (x2, y - m_cyChar, x3, y + m_cyChar);
  198.     pDC->SelectObject (pOldBrush);
  199. }
  200.